﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace 智信科技物流系统
{
    public partial class Release : Form
    {
        public Release()
        {
            InitializeComponent();
        }
        //黑白熊：看看这清楚简洁的代码块，这才是超高校级的程序员（张昊)应有的样子，噗哈哈~~~
        private void Release_Load(object sender, EventArgs e)
        {
            BindProvince();
            //动态按钮生成
            cizubutton();
            makebutton();
            //设定默认选项
            cmb_Sheng1.SelectedIndex = 2;
            cmb_Shi1.SelectedIndex = 1;
            cmb_Sheng2.SelectedIndex = cmb_Sheng3.SelectedIndex = 2;
            cmb_Shi2.SelectedIndex = cmb_Shi3.SelectedIndex = 1;
            cmb_city2.SelectedIndex = cmb_City3.SelectedIndex = 3;
            cmb_Sheng21.SelectedIndex = 2;
            cmb_Shi21.SelectedIndex = 1;
            cmb_Sheng22.SelectedIndex = cmb_Sheng23.SelectedIndex = 2;
            cmb_Shi22.SelectedIndex = cmb_Shi23.SelectedIndex = 1;
            cmb_city22.SelectedIndex = cmb_city23.SelectedIndex = 3;
            //获取用户信息
            GetUserini();
        }




        #region 动态按钮生成
        private void cizubutton()
        {
            int i = 0;
            int twidth = button14.Location.X;
            //第一行
            for (i = 0; i < Wordthing.cizu.Length; i++)
            {

                Button t = new Button();
                t.Width = (button14.Width - 3) * Wordthing.cizu[i].Length;
                t.TextAlign = button14.TextAlign;
                t.Name = "t" + i;
                t.Text = Wordthing.cizu[i];
                Point p = new Point(twidth, button14.Location.Y + button14.Height);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click);//添加事件
            }

            //  第二行
            twidth = button14.Location.X;
            for (i = 0; i < Wordthing.cizu2.Length; i++)
            {

                Button t = new Button();

                t.Width = button14.Width * Wordthing.cizu2[i].Length;

                t.Name = "t2" + i;
                t.Text = Wordthing.cizu2[i];
                Point p = new Point(twidth, button14.Location.Y + button14.Height * 2);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click);
            }

            //  第三行
            twidth = button14.Location.X;
            for (i = 0; i < Wordthing.cizu3.Length; i++)
            {

                Button t = new Button();

                t.Width = button14.Width * Wordthing.cizu3[i].Length;

                t.Name = "t2" + i;
                t.Text = Wordthing.cizu3[i];
                Point p = new Point(twidth, button14.Location.Y + button14.Height * 3);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click);
            }
            //  第四行
            twidth = button14.Location.X;
            for (i = 0; i < Wordthing.cizu4.Length; i++)
            {

                Button t = new Button();

                t.Width = button14.Width * Wordthing.cizu4[i].Length;

                t.Name = "t2" + i;
                t.Text = Wordthing.cizu4[i];
                Point p = new Point(twidth, button14.Location.Y + button14.Height * 4);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click);
            }

        }
        private void makebutton()
        {
            int i = 0;
            int twidth = button15.Location.X;
            //第一行
            for (i = 0; i < Wordthing.buttons1.Length; i++)
            {

                Button t = new Button();
                t.Width = (button14.Width - 6) * (Wordthing.buttons1[i].Length);
                t.TextAlign = button14.TextAlign;
                t.Name = "tt" + i;
                t.Text = Wordthing.buttons1[i];
                t.Font = button15.Font;
                Point p = new Point(twidth, button15.Location.Y);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click2);//添加事件
            }
            //第二行左
            twidth = button14.Location.X;
            for (i = 0; i < Wordthing.buttons2.Length; i++)
            {

                Button t = new Button();

                t.Width = (button14.Width - 6) * Wordthing.buttons2[i].Length;
                t.TextAlign = button14.TextAlign;
                t.Name = "tt2" + i;
                t.Text = Wordthing.buttons2[i];
                Point p = new Point(twidth, button15.Location.Y + button15.Height);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click2);
            }

            //第二行右

            for (i = 0; i < Wordthing.cizu0.Length; i++)
            {

                Button t = new Button();
                t.TextAlign = button14.TextAlign;
                if (Wordthing.cizu0[i].Length > 3)
                {
                    t.Width = (button14.Width - 3) * Wordthing.cizu0[i].Length;
                }
                else
                {
                    t.Width = (button14.Width) * Wordthing.cizu0[i].Length;
                }
                t.Name = "tt3" + i;
                t.Text = Wordthing.cizu0[i];
                Point p = new Point(twidth, button15.Location.Y + button15.Height);
                t.Location = p;
                panel2.Controls.Add(t);
                twidth += t.Width;
                t.Click += new System.EventHandler(btn_click);
            }
        }
        //处理动态按钮事件
        private void btn_click(object sender, EventArgs e)
        {
            Button b1 = (Button)sender;//将触发此事件的对象转换为该Button对象
            infor2.SelectedText = b1.Text;

        }
        private void btn_click2(object sender, EventArgs e)
        {
            Button b1 = (Button)sender;//将触发此事件的对象转换为该Button对象

            contextMenuStrip1.Items.Clear();
            /*   暂时先不设计
             * DBOperate operate = new DBOperate();
                operate.BindContext(b1.Text, contextMenuStrip1);
                */
            Point p = new Point(MousePosition.X, MousePosition.Y);
            contextMenuStrip1.Show(p);
        }
        #endregion

        #region 省市按钮的设定
        private void BindProvince()
        {
            for (int i = 0; i < Wordthing.Province.Rows.Count; i++)
            {

                if (Wordthing.Province.Rows[i]["AreaPid"].ToString().Equals("0"))
                {
                    string sheng = Wordthing.Province.Rows[i]["AreaName"].ToString();
                    cmb_Sheng1.Items.Add(sheng);
                    cmb_Sheng2.Items.Add(sheng);
                    cmb_Sheng21.Items.Add(sheng);
                    cmb_Sheng22.Items.Add(sheng);
                    cmb_Sheng23.Items.Add(sheng);
                    cmb_Sheng3.Items.Add(sheng);
                }
            }
        }
        //模板
        private void SelectedIndexChanged(ComboBox cbA, ComboBox cbB)
        {
            if (cbA.SelectedItem == null) return;
            cbB.Items.Clear();
            string AreaPid = "";
            for (int i = 0; i < Wordthing.Province.Rows.Count; i++)
            {
                if (Wordthing.Province.Rows[i]["AreaName"].ToString().Equals(cbA.SelectedItem.ToString()))
                {
                    AreaPid = Wordthing.Province.Rows[i]["AreaId"].ToString();
                    break;
                }

            }
            for (int i = 0; i < Wordthing.Province.Rows.Count; i++)
            {
                if (Wordthing.Province.Rows[i]["AreaPid"].ToString().Equals(AreaPid))
                {
                    cbB.Items.Add(Wordthing.Province.Rows[i]["AreaName"].ToString());
                }
            }

        }
        private void cmb_Sheng1_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng1, cmb_Shi1);
            cmb_Shi1.SelectedIndex = 0;
        }
        private void cmb_Sheng2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng2, cmb_Shi2);
            cmb_Shi2.SelectedIndex = 0;
        }
        private void cmb_Shi2_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Shi2, cmb_city2);
            cmb_city2.SelectedIndex = 0;
        }
        private void cmb_sheng21_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng21, cmb_Shi21);
            cmb_Shi21.SelectedIndex = 0;
        }
        private void cmb_Sheng22_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng22, cmb_Shi22);
            cmb_Shi22.SelectedIndex = 0;
        }
        private void cmb_Shi22_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Shi22, cmb_city22);
            cmb_city22.SelectedIndex = 0;
        }
        private void cmb_sheng23_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng23, cmb_Shi23);
            cmb_Shi23.SelectedIndex = 0;
        }
        private void cmb_Shi23_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Shi23, cmb_city23);
            cmb_city23.SelectedIndex = 0;
        }
        private void cmb_Sheng3_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Sheng3, cmb_Shi3);
            cmb_Shi3.SelectedIndex = 0;
        }
        private void cmb_Shi3_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedIndexChanged(cmb_Shi3, cmb_City3);
            cmb_City3.SelectedIndex = 0;
        }
        #endregion

        #region 用户个人信息的读取，checkbox
        private void GetUserini()
        {
            chk_Fax.Checked = (Userini.userinfor.Rows[0]["Fax"] != null && Userini.userinfor.Rows[0]["Fax"].ToString().Trim() != "");
            chk_Fix.Checked = (Userini.userinfor.Rows[0]["FixedTelephone"] != null && Userini.userinfor.Rows[0]["FixedTelephone"].ToString().Trim() != "");
            chk_Mobile.Checked = (Userini.userinfor.Rows[0]["MobilePhone"] != null && Userini.userinfor.Rows[0]["MobilePhone"].ToString().Trim() != "");
            chk_QQ.Checked = (Userini.userinfor.Rows[0]["QQ"] != null && Userini.userinfor.Rows[0]["QQ"].ToString().Trim() != "");

        }
        private void chk_Mobile1_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Mobile.Checked)
            {
                tb_Mobile.Text = Userini.userinfor.Rows[0]["MobilePhone"].ToString();
                tb_Mobile.Visible = true;
            }
            else
            {
                tb_Mobile.Text = "";
                tb_Mobile.Visible = false;
            }
        }

        private void chk_QQ_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_QQ.Checked)
            {
                tb_QQ.Text = Userini.userinfor.Rows[0]["QQ"].ToString();
                tb_QQ.Visible = true;
            }
            else
            {
                tb_QQ.Visible = false;
                tb_QQ.Text = "";
            }
        }

        private void chk_Fix_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Fix.Checked)
            {
                tb_Fix.Text = Userini.userinfor.Rows[0]["FixedTelephone"].ToString();
                tb_Fix.Visible = true;
            }
            else
            {
                tb_Fix.Visible = false;
                tb_Fix.Text = "";
            }
        }

        private void chk_Fax_CheckedChanged(object sender, EventArgs e)
        {
            if (chk_Fax.Checked)
            {
                tb_Fax.Text = Userini.userinfor.Rows[0]["Fax"].ToString();
                tb_Fax.Visible = true;
            }
            else
            {
                tb_Fax.Visible = false;
                tb_Fax.Text = "";
            }
        }
        #endregion

        //生成添加按钮
        private void btn_ADD_Click(object sender, EventArgs e)
        {
            string str = "";
            str += "到达城市：" + cmb_Sheng3.Text + " " + cmb_Shi3.Text + " " + cmb_City3.Text + "\r\n";
            if (initype1.Checked)
            {
                str += "有" + line11.Text + line12.Text + line13.Text + "\r\n";
                str += "求" + line21.Text + "辆" + line22.Text + "米" + line23.Text + "\r\n";
            }
            else
            {
                str += "有" + line21.Text + "辆" + line22.Text + "米" + line23.Text + "\r\n";
                str += "求" + line11.Text + line12.Text + line13.Text + "\r\n";
            }

            str += line31.Text;
            if (line32.Text != "")
            {
                str += "  " + line32.Text + "元/" + line33.Text;
            }
            tb_infor1.Text = str;
        }

        //发布信息
        private void btn_fabu_Click(object sender, EventArgs e)
        {
            //判断信息类型
            if (initype1.Checked == initype2.Checked)
            {
                MessageBox.Show("请先选择信息类型");
                //label9.ForeColor = Color.Red;
                //initype1.BackColor = initype2.BackColor = Color.Yellow;
                //label18.ForeColor = Color.Red;
                //initype21.BackColor = initype22.BackColor = Color.Yellow;
                return;
            }
            //信息完整判断
            if (!inijudge()) return;


            string InformationType = "";
            string ReleaseTo = "";
            string DepartureCity = "";
            string ReachCity = "";
            string AutoRetransmission = "";
            string information = "";
            if (tabControl1.SelectedIndex == 0)
            {
                //发布信息设置
                InformationType = initype1.Checked ? initype1.Text : initype2.Text;
                ReleaseTo = cmb_Sheng1.Text + "," + cmb_Shi1.Text;
                DepartureCity = cmb_Sheng2.Text + "," + cmb_Shi2.Text + "," + cmb_city2.Text;
                ReachCity = cmb_Sheng3.Text + "," + cmb_Shi3.Text + "," + cmb_City3.Text;
                AutoRetransmission = repeat.SelectedIndex.ToString();
                information = tb_infor1.Text.Trim().ToString();
            }
            else if (tabControl1.SelectedIndex == 1)
            {
                InformationType = initype21.Checked ? initype21.Text : initype22.Text;
                ReleaseTo = cmb_Sheng21.Text + "," + cmb_Shi21.Text;
                DepartureCity = cmb_Sheng22.Text + "," + cmb_Shi22.Text + "," + cmb_city22.Text;
                ReachCity = cmb_Sheng23.Text + "," + cmb_Shi23.Text + "," + cmb_city23.Text;
                AutoRetransmission = repeat.SelectedIndex.ToString();
                information = infor2.Text.Trim().ToString();
            }

            string fabusql = "insert into bm_Release (InformationType,ReleaseTo,DepartureCity,ReachCity,InformationContent,MobilePhone,FixedTelephone,Fax,QQ,AutoRetransmission,UserName,DepartureTime) values (N'"
                 + InformationType + "',N'" + ReleaseTo + "',"
                 + "N'" + DepartureCity + "'," + "N'" + ReachCity + "',"
                 + "N'" + information + "'," + "N'"
                 + this.tb_Mobile.Text.Trim().ToString() + "'," + "N'" + this.tb_Fix.Text.Trim().ToString()
                 + "'," + "N'" + this.tb_Fax.Text.Trim().ToString() + "'," + "N'" + this.tb_QQ.Text.Trim().ToString() + "',"
                 + "N'" + AutoRetransmission + "',N'" + Userini.UserName + "',N'" + DateTime.Now.ToString("yyyy-MM-dd HH:mm") + "')";

            Dao dao = new Dao();
            if (dao.sqlInsert(fabusql, "发布信息失败") > 0)
                MessageBox.Show("发布成功");
            else
                MessageBox.Show("发布失败");

            if (repeat.SelectedIndex > 0)
            {
                //为重发管理赋值
                repeatini.initype.Add(InformationType);
                repeatini.inicontent.Add(this.tb_infor1.Text.Trim().ToString());
                repeatini.initime.Add(repeat.SelectedIndex * 5);
                repeatini.ininum.Add(repeatnum.SelectedIndex + 1);
                repeatini.id.Add(repeatini.inicontent.Count - 1);

                Thread t_fabu = new Thread(Repeat);
                t_fabu.IsBackground = true;
                t_fabu.Start(fabusql);
            }

        }

        private void Repeat(object cmd)
        {
            int id = repeatini.inicontent.Count - 1;//记录当前线程位于list中的位置
            int sum = repeatini.ininum[id];
            int time = repeatini.initime[id] * 1000* 60;//时间数据转换成毫秒     测试期间不加 !!!!*60!!!!!
            string sql = (string)cmd;//建立线程中的局部变量,保证新建的重发的内容不会冲突
           
            while (sum > 0)
            {
                Thread.Sleep(time);

                if (repeatini.ininum[id] < 1)//当重发次数为零,先判断
                {
                    //删除一条数据
                    repeatini.initype.RemoveAt(id);
                    repeatini.inicontent.RemoveAt(id);
                    repeatini.initime.Remove(id);
                    repeatini.ininum.RemoveAt(id);
                    sum = 0;//终止重发  
                    break;
                }
                Dao dao = new Dao();
                int i = dao.sqlInsert(sql,"信息:\n"+repeatini.inicontent+"\n 重发失败");
                repeatini.ininum[id] -= 1;

                //重发判断
                if (i > 0)
                {
                    //MessageBox.Show("发布成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    sum--;//次数减1
                }
                else
                {
                    MessageBox.Show("本次重发失败！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }  
        }


        //发布信息完整判断
        private bool inijudge()
        {
            if (tabControl1.SelectedIndex == 0)
            {
                if (tb_infor1.Text.Trim() == "")
                {
                    MessageBox.Show("填写信息内容");
                    return false;
                }
            }
            if (tabControl1.SelectedIndex == 1)
            {
                if (infor2.Text.Trim() == "")
                {
                    MessageBox.Show("填写信息内容");
                    return false;
                }
            }
            return true;
        }

        private void repeat_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (repeat.SelectedIndex != 0)
            {
                repeatnum.Visible = true;
                repeatnum.SelectedIndex = 0;
               // repeattime = repeat.SelectedIndex * 5;

            }
            else
            {
                repeatnum.Visible = false;
            }
        }

        private void btn_Repeat_Click(object sender, EventArgs e)
        {
            Repeat frm_repeat = new Repeat();
            frm_repeat.Show();
        }

    }
}
